package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Product struct {
	gorm.Model
	Code string
	Price uint
}

type User struct {
	gorm.Model
	Name string
	Age uint
	Birthday string
}


func My_first_table()  {
	fmt.Println("My_first_table   create table Product")
}


func main() {
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
//dsn := "user:pass@tcp(192.168.42.200:3306)/portscan?charset=utf8mb4&parseTime=True&loc=Local"
dsn := "root:root@tcp(192.168.42.200:3306)/portscan?charset=utf8mb4"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

if err != nil {
	fmt.Println("err")
}

if db != nil {
	fmt.Println("db")

}
My_first_table()

// Migrate the schema
db.AutoMigrate(&Product{})
db.Create(&Product{Code: "L1212", Price: 1000})
// 读取
var product Product
db.First(&product, 1) // 查询id为1的product
db.First(&product, "code = ?", "L1212") // 查询code为l1212的product

// 更新 - 更新product的price为2000
db.Model(&product).Update("Price", 2000)

// 删除 - 删除product
//db.Delete(&product)



user := User{Name: "Jinzhu", Age: 18, Birthday: "sss"}

db.Create(&user) // pass pointer of data to Create

fmt.Println(user.Name)
// returns inserted data's primary key
//result.Error        // returns error
//result.RowsAffected //
}




//user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
//
//result := db.Create(&user) // pass pointer of data to Create
//
//user.ID             // returns inserted data's primary key
//result.Error        // returns error
//result.RowsAffected // returns inserted records count